package com.webshop.servlet;

import com.webshop.Dao.ProductDao;
import com.webshop.domain.Items;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@WebServlet("/ChangeProServlet")
public class ChangeProServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        String method = req.getParameter("method");
        String id = req.getParameter("id");
        if (method.equals("pic")){
            String path = req.getServletContext().getRealPath("/img");
            DiskFileItemFactory factory = new DiskFileItemFactory();
            ServletFileUpload upload = new ServletFileUpload(factory);
            upload.setHeaderEncoding("utf-8");
            try {
                List<FileItem> list = upload.parseRequest(req);
                for (FileItem item: list){
                    String imgName = item.getName();
                    imgName = imgName.substring(imgName.indexOf('.'));
                    imgName = id+imgName;
                    String pimgsrc = "img/"+imgName;
                    InputStream is = item.getInputStream();
                    FileOutputStream fos = new FileOutputStream(new File(path+"/"+imgName));
                    byte[] buffer = new byte[1024];
                    int len = 0;
                    while ((len=(is.read(buffer)))>-1){
                        fos.write(buffer,0,len);
                    }
                    ProductDao.update("pimgsrc",pimgsrc,Integer.parseInt(id));
                    fos.close();
                    is.close();
                }
            } catch (FileUploadException e) {
                e.printStackTrace();
            }
            resp.sendRedirect("change.jsp?id="+id);
        }
        if (method.equals("price")){
            String price = req.getParameter("price");
            ProductDao.update("pprice",price,Integer.parseInt(id));
            resp.sendRedirect("change.jsp?id="+id);
        }
        if (method.equals("name")){
            String name = req.getParameter("name");
            ProductDao.update("pname",name,Integer.parseInt(id));
            resp.sendRedirect("change.jsp?id="+id);
        }
        if (method.equals("info")){
            String info = req.getParameter("info");
            ProductDao.update("pinfo",info,Integer.parseInt(id));
            resp.sendRedirect("change.jsp?id="+id);
        }
        if(method.equals("add")){
            String attr = "0";
            String attrv = "0";
            if (ProductDao.getItem(Integer.parseInt(req.getParameter("id"))).getAttr() !=null){
                attr = ProductDao.getItem(Integer.parseInt(req.getParameter("id"))).getAttr() + "#0";
                attrv = ProductDao.getItem(Integer.parseInt(req.getParameter("id"))).getAttrv() + "#0";
            }
            ProductDao.update("attr",attr,Integer.parseInt(id));
            ProductDao.update("attrv",attrv,Integer.parseInt(id));
            resp.sendRedirect("change.jsp?id="+id);
        }
        if (method.equals("delete")){
            Items items = ProductDao.getItem(Integer.parseInt(id));
            List<String> attrs = new ArrayList<String>(Arrays.asList(items.getAttr().split("#")));
            List<String> attrvs = new ArrayList<String>(Arrays.asList(items.getAttrv().split("#")));
            int n = Integer.parseInt((String)req.getParameter("n"));
            attrs.remove(n);
            attrvs.remove(n);
            if (attrs.size()==0){
                ProductDao.update("attr","",Integer.parseInt(id));
                ProductDao.update("attrv","",Integer.parseInt(id));
                resp.sendRedirect("change.jsp?id="+id);
                return;
            }
            String attr = attrs.get(0);
            String attrv = attrvs.get(0);
            for (int i = 1;i<attrs.size();i++){
                attr = attr+"#"+attrs.get(i);
                attrv = attrv+"#"+attrvs.get(i);
            }
            ProductDao.update("attr",attr,Integer.parseInt(id));
            ProductDao.update("attrv",attrv,Integer.parseInt(id));
            resp.sendRedirect("change.jsp?id="+id);
            return;
        }
        if (method.equals("change")){
            Items items = ProductDao.getItem(Integer.parseInt(req.getParameter("id")));
            String who = req.getParameter("who");
            int num = Integer.parseInt(req.getParameter("num"));
            if (who.equals("attr")){
                List<String> attrs = new ArrayList<String>(Arrays.asList(items.getAttr().split("#")));
                String attr = req.getParameter("attr");
                attrs.set(num,attr);
                String nattr = attrs.get(0);
                for (int i = 1;i<attrs.size();i++){
                    nattr = nattr+"#"+attrs.get(i);
                }
                ProductDao.update("attr",nattr,Integer.parseInt(id));

            }
            if (who.equals("attrv")){
                List<String> attrvs = new ArrayList<String>(Arrays.asList(items.getAttrv().split("#")));
                String attrv = req.getParameter("attrv");
                attrvs.set(num,attrv);
                String nattrv = attrvs.get(0);
                for (int i = 1;i<attrvs.size();i++){
                    nattrv = nattrv+"#"+attrvs.get(i);
                }
                ProductDao.update("attrv",nattrv,Integer.parseInt(id));

            }
            resp.sendRedirect("change.jsp?id="+id);
        }
        if (method.equals("ppclass")){
            String ppclass = req.getParameter("ppclass");
            ProductDao.update("ppclass",ppclass,Integer.parseInt(id));
            resp.sendRedirect("change.jsp?id="+id);
        }
        if (method.equals("sinfo")){
            String sinfo = req.getParameter("sinfo");
            ProductDao.update("sinfo",sinfo,Integer.parseInt(id));
            resp.sendRedirect("change.jsp?id="+id);
        }
    }
}
